<?php

namespace App\Listeners;

use App\Events\OrderIntegral;
use App\Integral;
use App\User;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;

class HandleOrderIntegral
{
    /**
     * Create the event listener.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }

    /**
     * Handle the event.
     *
     * @param  OrderIntegral  $event
     * @return void
     */
    public function handle(OrderIntegral $event)
    {
        $order = $event->order;
        $user = User::find($order->user_id);
        $orderGoods =  $order->orderGoods;
        $integral = 0;
        foreach ($orderGoods as $oGoods) {
            $integral += $oGoods->goods->give_integral;
        }
        if ($integral > 0) {
            $user->integral = $user->integral + $integral;
            $user->save();
            Integral::create(['user_id' => $user->id, 'number' => $integral, 'type' => 1, 'description' => '购物，获得成长值']);
        }
    }
}
